//
//  ChangePhoneFirstViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/8.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ChangePhoneFirstViewController.h"
#import "ChangePhoneSecondViewController.h"
#import "HttpRequest.h"
#import "ChangePasswordSecondViewController.h"

@interface ChangePhoneFirstViewController ()

@property (strong, nonatomic) UITextField *password;
@property (strong, nonatomic) UIButton *getCodeButton;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) double timertime;
@property (assign, nonatomic) BOOL shouldRemove;
@end

@implementation ChangePhoneFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"账户安全";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, XNavigationHeight + 10, ScreenWidth - 30, 21)];
    title.text = @"为确保是您本人操作，请完成以下验证：";
    title.font = [UIFont systemFontOfSize:14.0];
    title.textColor = COLOR(63, 63, 63, 1);
    [self.view addSubview:title];
    
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(title.frame) + 20, ScreenWidth - 30, 34)];
    text.textColor = COLOR(63, 63, 63, 1);
    text.font = [UIFont systemFontOfSize:14.0];
    text.textAlignment = NSTextAlignmentCenter;
    text.numberOfLines = 2;
    text.text = [NSString stringWithFormat:@"我们已经发送校验码到你的手机：\n%@", [UserModel shared].loginUser.mobile];
    [self.view addSubview:text];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(text.frame) + 20, ScreenWidth - 30, 47)];
    _password.backgroundColor = COLOR(235, 235, 235, 1);
    _password.placeholder = @"请输入验证码";
    [self.view addSubview:_password];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(_password.frame) + 30.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"下一步" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
    
    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _getCodeButton.frame = CGRectMake(0, 0, 113, 33);
    _getCodeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_getCodeButton setTitleColor:COLOR(151, 151, 151, 1) forState:UIControlStateNormal];
    _getCodeButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getCodeButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    [_getCodeButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    _password.rightView = _getCodeButton;
    _password.rightViewMode = UITextFieldViewModeAlways;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    view.backgroundColor = COLOR(235, 235, 235, 1);
    _password.leftView = view;
    _password.leftViewMode = UITextFieldViewModeAlways;
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)done {
    
    if (_password.text.length > 0) {
        
        [self.view endEditing:YES];
        __weak typeof(self) weakself = self;
        [HttpRequest postURL:kcheck_code_account paras:@{@"code": _password.text} showProgress:NO successful:^(id  _Nullable result) {
            
            if (weakself.type == 0) {
                ChangePhoneSecondViewController *con = [[ChangePhoneSecondViewController alloc] init];
                weakself.shouldRemove = self;
                [weakself.navigationController pushViewController:con animated:YES];
            }else {
                ChangePasswordSecondViewController *con = [[ChangePasswordSecondViewController alloc] init];
                weakself.shouldRemove = YES;
                [weakself.navigationController pushViewController:con animated:YES];
            }
            
        } failed:nil];
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (_shouldRemove) {
        [self removeSelf];
    }
}

- (void)removeSelf {
    
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    for (NSInteger i = 0; i < controllers.count; i ++) {
        UIViewController *controller = controllers[i];
        if ([controller isKindOfClass:[ChangePhoneFirstViewController class]]) {
            [controllers removeObject:controller];
            break;
        }
    }
    self.navigationController.viewControllers = controllers;
}

- (void)getCode {
    
    [self.view endEditing:YES];
    [HttpRequest postURL:kget_code_account paras:nil showProgress:NO successful:^(id  _Nullable result) {
        
    } failed:nil];
    _timertime = 60;
    [self startTimer];
}

- (void)startTimer {
    
    _getCodeButton.enabled = NO;
    [_getCodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

- (void)timerMethod {
    
    if (_timertime <= 0) {
        [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
        [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeButton.enabled = YES;
        [self.timer invalidate];
        return;
    }
    _timertime = _timertime - 1;
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
